package cn.com.shopec.erp.system.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.core.base.controller.BaseController;
import cn.com.shopec.erp.system.constant.SystemConstant;
import cn.com.shopec.erp.system.entity.Role;
import cn.com.shopec.erp.system.service.IAccessService;
import cn.com.shopec.erp.system.service.IRoleService;
import cn.com.shopec.erp.system.vo.AccessVo;
import cn.com.shopec.erp.system.vo.RoleVo;

/**
 * 
 * @ClassName RoleController
 * @Description 角色Controller
 * @author chengui
 * @date 2013-5-30 上午10:56:44
 */
@Controller
@RequestMapping("/manager/system/role")
public class RoleController extends BaseController {
	
	@Resource private IRoleService roleService;
	@Resource private IAccessService accessService;
	
	/**
	 * 
	 * @MethodName roleList
	 * @Description 供应链角色列表
	 * @author chengui
	 * @date 2013-5-30 下午4:29:26
	 * @param userVo
	 * @param modelMap
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("roleList")
	public ModelAndView roleList(RoleVo roleVo, ModelMap modelMap) throws Exception {
		modelMap.put("roleVo", roleVo);
		return new ModelAndView("system/role/role_list");
	}
	
	/**
	 * 
	 * @MethodName ajaxToRoleList
	 * @Description ajax分页查询角色列表
	 * @author chengui
	 * @date 2013-5-30 下午4:47:03
	 * @param roleVo
	 * @param pageQuery
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping("ajaxToRoleList")
	@ResponseBody
	public String ajaxToRoleList(RoleVo roleVo, PageQuery pageQuery) throws Exception {
		PageFinder<RoleVo> pageFinder = roleService.getPagedRoleVos(roleVo, pageQuery);
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("Rows", pageFinder.getData());
		jsonObj.put("Total", pageFinder.getRowCount());
		return jsonObj.toJSONString();
	}
	
	/**
	 * 
	 * @MethodName toAddRole
	 * @Description 新增供应链角色页面
	 * @author chengui
	 * @date 2013-5-30 下午5:13:54
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("toAddRole")
	public ModelAndView toAddRole() throws Exception {
		return new ModelAndView("system/role/add_role");
	}
	
	/**
	 * 
	 * @MethodName addRole
	 * @Description 新增角色
	 * @author chengui
	 * @date 2013-5-30 下午6:30:18
	 * @param userVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("addRole")
	@ResponseBody
	public String addRole(RoleVo roleVo) throws Exception {
		roleService.addRole(roleVo);
		return SystemConstant.SUCCESS;
	}
	
	/**
	 * 
	 * @MethodName roleCodeExist
	 * @Description 检查角色编码是否已经存在
	 * @author chengui
	 * @date 2013-5-30 下午6:50:19
	 * @param roleVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("roleCodeExist")
	@ResponseBody
	public String roleCodeExist(RoleVo roleVo) throws Exception {

    	if(null!= roleVo && LogicUtil.isNotNullAndEmpty(roleVo.getRoleCode())){
    		List<Role> list = roleService.roleListByRoleCode(roleVo.getRoleCode());
    		if(null != list && list.size()>0){
    			return SZSCMConstant.FAIL;
    		}
    	}
         return SZSCMConstant.SUCCESS;
	}
	
	/**
	 * 
	 * @MethodName toModifyRole
	 * @Description 修改供应链角色页面
	 * @author chengui
	 * @date 2013-5-30 下午7:09:11
	 * @param roleVo
	 * @param modelMap
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("toModifyRole")
	public ModelAndView toModifyRole(RoleVo roleVo, ModelMap modelMap) throws Exception {
		RoleVo rv = roleService.getRoleVo(roleVo);
		modelMap.put("roleVo", rv);
		return new ModelAndView("system/role/modify_role");
	}
	
	/**
	 * 
	 * @MethodName modifyRole
	 * @Description 修改供应链角色
	 * @author chengui
	 * @date 2013-5-30 下午7:33:04
	 * @param roleVo
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping("modifyRole")
	@ResponseBody
	public String modifyRole(RoleVo roleVo) throws Exception {
		roleService.modifyRole(roleVo);
		return SystemConstant.SUCCESS;
	}
	
	/**
	 * 
	 * @MethodName deleteRole
	 * @Description 删除角色
	 * @author chengui
	 * @date 2013-5-30 下午7:38:59
	 * @param roleVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("deleteRole")
	@ResponseBody
	public String deleteRole(RoleVo roleVo) throws Exception {
		roleService.deleteRole(roleVo);
		return SystemConstant.SUCCESS;
	}
	
	/**
	 * 
	 * @MethodName toAllocateAccess
	 * @Description 分配权限页面
	 * @author chengui
	 * @date 2013-6-1 下午3:49:18
	 * @param roleVo
	 * @param modelMap
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("toAllocateAccess")
	public ModelAndView toAllocateAccess(RoleVo roleVo, ModelMap modelMap) throws Exception {
		RoleVo rv = roleService.getRoleVo(roleVo);
		modelMap.put("roleVo", rv);
		return new ModelAndView("system/role/allocate_access");
	}
	
	/**
	 * 
	 * @MethodName ajaxLoadAccessTree
	 * @Description 异步加载菜单树
	 * @author chengui
	 * @date 2013-6-1 下午4:19:37
	 * @param accessVo
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping("ajaxLoadAccessTree")
	@ResponseBody
	public String ajaxLoadAccessTree(AccessVo accessVo) throws Exception {
		RoleVo roleVo = roleService.getRoleVo(new RoleVo(accessVo.getRoleId()));
		List<AccessVo> accessVos = accessService.getMenuVos(accessVo, true, roleVo.getRoleType());
		return JSONObject.toJSONString(accessVos);
	}
	
	/**
	 * 
	 * @MethodName allocateAccess
	 * @Description 分配权限
	 * @author chengui
	 * @date 2013-6-1 下午5:32:54
	 * @param roleVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("allocateAccess")
	@ResponseBody
	public String allocateAccess(RoleVo roleVo) throws Exception {
		roleService.allocateAccess(roleVo);
		return SystemConstant.SUCCESS;
	}
	
    /**
     * 验证用户名
     * @throws Exception 
     * 
     * */
    @ResponseBody
    @RequestMapping("checkRoleName")
    public String checkRoleName(String roleName) throws Exception
    {
    	if(null!= roleName && LogicUtil.isNotNullAndEmpty(roleName)){
    		List<Role> list = roleService.roleList(roleName);
    		if(null != list && list.size()>0){
    			return SZSCMConstant.FAIL;
    		}
    	}
         return SZSCMConstant.SUCCESS;
    }
}
